/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.admin.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ekeyman.admin.util.JSONUtil;
import com.ekeymanlib.business.KeyManager;
import com.ekeymanlib.dto.AppDeviceGrid;
import com.ekeymanlib.dto.SearchFilter;
import com.opensymphony.xwork2.ActionSupport;

public class SearchAppDeviceAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7628656895060226873L;
	private static final Log  log = LogFactory.getLog(SearchAppDeviceAction.class);

	private List<AppDeviceGrid> gridModel;
	private KeyManager keyManager;
	private Integer rows = 0;
	private Integer page = 0;
	private String sord = "asc";
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private Integer total = 0;
	private Integer records = 0;
	private String filters;
	
	/*
	 * Populate the values needed by the searchAppDevice JQuery grid located on dashboard.jsp
	 */
	public String execute(){
		
	    int to = (rows * page);
	    int from = to - rows;

	    
		List<SearchFilter> searchFilters = JSONUtil.buildSearchFilter(filters);
		
	    records = ((Long)keyManager.getAppDeviceCount(searchFilters)).intValue(); 
	    
	    gridModel = keyManager.findAppDevices(searchFilters, 
	    		getSidx(), getSord(), from, to);
	    
	    if (to > records) {
	    	to = records;
	    }

	    total = (int) Math.ceil((double) records / (double) rows);
	    
		return SUCCESS;
	}
	
	public String getJSON(){
		return execute();
	}

	public List<AppDeviceGrid> getGridModel() {
		return gridModel;
	}

	public void setGridModel(List<AppDeviceGrid> gridModel) {
		this.gridModel = gridModel;
	}

	/*
	 * BE CAREFUL! - When the action is serialized as a JSON object all getters are
	 * executed. While traversing the object graph for keyManager, JSON will encounter
	 * objects (i.e. Hibernate object) that cannot be serialized. The exception will go
	 * undetected as JQGrid has no way to report it!
	 * 
	public KeyManager getKeyManager() {
		return keyManager;
	}
	*/

	public void setKeyManager(KeyManager keyManager) {
		this.keyManager = keyManager;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
	    this.records = records;

	    if (this.records > 0 && this.rows > 0)
	    {
	      this.total = (int) Math.ceil((double) this.records / (double) this.rows);
	    }
	    else
	    {
	      this.total = 0;
	    }
	}

	/*
	public String getFilters() {
		return filters;
	}
*/
	public void setFilters(String filters) {
		this.filters = filters;
	}

}
